package com.bub.pay.core.manager.email.impl;

import com.bub.pay.core.manager.client.ClientManager;
import com.bub.pay.core.manager.email.EmailService;
import com.bub.pay.framework.common.base.Cause;
import com.bub.pay.framework.common.base.Module;
import com.bub.pay.framework.common.config.UserConfig;
import com.bub.pay.framework.common.entity.model.SessionModel;
import com.bub.pay.framework.common.exception.GlobalException;
import com.bub.pay.framework.common.template.EmailTemplate;
import com.bub.pay.framework.common.util.ContextHolder;
import com.bub.pay.framework.redis.RedisClient;
import com.bub.pay.framework.redis.RedisKeyPrefix;
import com.bub.pay.framework.redis.model.EmailModel;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private RedisClient redisClient;
    @Resource
    private UserConfig userConfig;
    @Resource
    private ClientManager clientManager;

    @Override
    public void sendEmailCode(String email, Module module) {

        if (StringUtils.isBlank(email)){
            throw new GlobalException(Cause.operat_fail);
        }
        SessionModel sessionModel = ContextHolder.currentUserInfo();

        String randomNumber = "";
        String key = sessionModel.getUserId() + ":" + email +":EmailCode";
        if (redisClient.exists(RedisKeyPrefix.EMAILCODE , key)){
            randomNumber = redisClient.get(RedisKeyPrefix.EMAILCODE, key).toString();
            redisClient.setExpiredTime(RedisKeyPrefix.EMAILCODE, key, userConfig.getEmailVerificationCodeExpire());
        }else {
            randomNumber = RandomStringUtils.randomNumeric(6);
        }

        String systemName = Module.getName(module);

        Map<String, String> data = new HashMap<>();
        data.put("userName", sessionModel.getNickName());
        data.put("userId", sessionModel.getUserId().toString());
        data.put("newEmail", email);
        data.put("oldEmail", sessionModel.getEmail());
        data.put("verificationCode", randomNumber);
        data.put("systemName", systemName);
        EmailModel emailModel = new EmailModel();
        emailModel.setAddress(email);
        emailModel.setData(data);
        emailModel.setTemplateId(EmailTemplate.TEMPLATE_EMAIL_VERIFICATION_CODE);
        clientManager.sendEmail(emailModel);

        redisClient.set(RedisKeyPrefix.EMAILCODE, key , randomNumber, userConfig.getEmailVerificationCodeExpire());
    }

    @Override
    public void checkEmailCode(String email, String code) {
        SessionModel sessionModel = ContextHolder.currentUserInfo();
        String key = sessionModel.getUserId() + ":" + email +":EmailCode";
        if (!redisClient.exists(RedisKeyPrefix.EMAILCODE , key)){
            throw new GlobalException(Cause.update_user_email_and_email_code_is_expired);
        }

        String randomNumber = redisClient.get(RedisKeyPrefix.EMAILCODE, key).toString();
        if (!StringUtils.equals(randomNumber,code)){
            throw new GlobalException(Cause.update_user_email_and_email_code_is_error);
        }
        redisClient.delete(RedisKeyPrefix.EMAILCODE, key);
    }
}
